/**
 * CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

package com.cmcc.zysoft.infocollection.mobile.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cmcc.zysoft.framework.utils.UUIDUtil;
import com.cmcc.zysoft.infocollection.common.BaseController;
import com.cmcc.zysoft.infocollection.mobile.service.MInterfaceLogService;
import com.cmcc.zysoft.infocollection.mobile.service.MSystemUserService;
import com.cmcc.zysoft.infocollection.model.SystemUser;
import com.cmcc.zysoft.infocollection.util.MD5Tools;
import com.cmcc.zysoft.infocollection.util.SendMsgTest;

/**
 * @author 袁凤建
 * <br />邮箱: yuan.fengjian@ustcinfo.com
 * <br />描述: SendMsgController.java
 * <br />版本: 1.0.0
 * <br />日期: 2013-9-24 上午9:37:28
 * <br />CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

@Controller
@RequestMapping("/mobile/msg")
public class SendMsgController extends BaseController {
	
	/**
	 * 属性名称: mSystemUserService.
	 * 类       型: MSystemUserService.
	 */
	@Resource
	private MSystemUserService mSystemUserService;
	
	/**
	 * 属性名称: mInterfaceLogService.
	 * 类       型: MInterfaceLogService.
	 */
	@Resource
	private MInterfaceLogService mInterfaceLogService;
	
	/**
	 * 发送短信-忘记密码.
	 * @param usercode 手机号
	 * @return map
	 */
	@RequestMapping("/sendMsg.htm")
	@ResponseBody
	@Transactional
	public Map<String, Object> sendMsg(String usercode) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<SystemUser> list = this.mSystemUserService.findByNamedParam(new String[]{"userName", "delFlag"}, new Object[]{usercode, "0"});
		if(null == list || list.isEmpty()) {
			//用户不存在或已删除
			map.put("code", "0");
		} else {
			SystemUser systemUser = list.get(0);
			String salt = UUIDUtil.generateUUID();
			int number = (int)((Math.random() + 1) * 1000000);
			String code = "" + number;
			code = code.substring(1, 7);
			String passWord = MD5Tools.encodePassword(code, salt);
			String content = "尊敬的信息采集助手用户，您的新密码是: " + code + "。输入密码即可登录。温馨提示：请勿将密码外泄！";
			String result = SendMsgTest.sendMsg(content, usercode);
			if("SUCCESS".equals(result)) {
				//更新用户密码
				systemUser.setPassSalt(salt);
				systemUser.setPassword(passWord);
				this.mSystemUserService.updateEntity(systemUser);
				//添加日志
				this.mInterfaceLogService.addLog(systemUser.getUserId(), systemUser.getCompanyId(), "忘记密码");
				//短信发送成功
				map.put("code", "1");
			} else {
				//短信发送失败-网络异常等
				map.put("code", "2");
			}
		}
		return map;
	}
}